<?php

use think\facade\Env;

    if(!function_exists("get_404_content")){
        function get_404_content(){
            $path = Env::get("app_path") . "/../public/404.html";
            $content = file_get_contents($path);
            return $content;
        }
    }
    if (!function_exists("get_sysconfig")) {
        function get_sysconfig($key = "")
        {
            return \app\common\service\SysConfigService::getConfig($key);
        }
    }

    /**
     * json响应
     */
    if (!function_exists("success_json_response")) {
        function success_json_response($msg = "", $data = [], $ext = [], $code = "")
        {
            return \app\common\service\ResponseService::successJsonResponse($msg, $data, $ext, $code);
        }
    }

    if (!function_exists("json_response")) {
        function json_response($msg = "", $data = [], $ext = [], $code = "")
        {
            return \app\common\service\ResponseService::JsonResponse($msg, $data, $ext, $code);
        }
    }

    if (!function_exists("filter_params")) {
        function filter_params($params = [], $filter_arr = [])
        {
            if (is_string($filter_arr)) {
                $filter_arr = explode(",", $filter_arr);
            }
            foreach ($params as $key => $row) {
                if (in_array($key, $filter_arr)) {
                    unset($params[$key]);
                }
            }
            return $params;
        }
    }

    if (!function_exists("filter_pagination")) {
        function filter_pagination($page, $limit)
        {
            $page = !empty($page) && is_numeric($page) && $page >= 1 ? floor($page) : 1;
            $limit = !empty($limit) && is_numeric($limit) && $limit >= 1 ? floor($limit) : 10;
            $start = ($page - 1) * $limit;
            return ['page' => $page, 'start' => $start, 'limit' => $limit];
        }
    }


    if (!function_exists("delete_dir_file")) {
        function delete_dir_file($dir_name)
        {
            $result = false;
            if (is_dir($dir_name)) {
                if ($handle = opendir($dir_name)) {
                    while (false !== ($item = readdir($handle))) {
                        if ($item != '.' && $item != '..') {
                            if (is_dir($dir_name . DIRECTORY_SEPARATOR . $item)) {
                                delete_dir_file($dir_name . DIRECTORY_SEPARATOR . $item);
                            } else {
                                @unlink($dir_name . DIRECTORY_SEPARATOR . $item);
                            }
                        }
                    }
                    closedir($handle);
                    if (rmdir($dir_name)) {
                        $result = true;
                    }
                }
            }
            return $result;
        }
    }


    if (!function_exists('current_datetime')) {
        /**
         * 格式化日期
         * @param string $datetime 日期时间
         * @return number[]|mixed[]
         */
        function current_datetime($formate = 'Y-m-d H:i:s')
        {
            return date($formate);
        }
    }

    if (!function_exists("time_format")) {
        function time_format($unix_time, $format = 'Y-m-d H:i:s')
        {
            if ($unix_time == 0) {
                return '--';
            }
            return date($format, $unix_time);
        }
    }

    if (!function_exists("format_bytes")) {
        function format_bytes($size)
        {
            $units = array('字节', 'K', 'M', 'G', 'T');
            $i = 0;
            for (; $size >= 1024 && $i < count($units); $i++) {
                $size /= 1024;
            }
            return round($size, 2) . $units[$i];
        }
    }

    if (!function_exists("get_allow_upload_ext")) {
        function get_allow_upload_ext($split = ",")
        {
            $arr = get_sysconfig("upload_ext");
            $arr = explode(",", $arr);
            if (is_array($split)) {
                return $arr;
            } else {
                return implode($split, $arr);
            }
        }
    }


    /**
     * 判断域名是否是绝对路径并返回绝对路径
     * @param string $domain 域名
     * @param string $path 路径
     * @param string $replace 替换
     * @return $string
     */
    if (!function_exists('get_http_path')) {
        function get_http_path($path, $domain = "", $replace = '')
        {
            if (empty($domain)) {
                $domain = get_sysconfig("site_domain");
            }
            if ($path) {
                if (strstr($path, 'http://') == false && strstr($path, 'https://') == false) {
                    return trim($domain, "/") . "/" . trim($path, "/");
                }
                return $path;
            } else {
                return $replace;
            }
        }
    }


    /**
     * 将文件夹打包成 zip压缩包
     *   $strPath =  Env::get('root_path') . "public/static/activity_rank/" . date("YmdHis") . $acvitity_model->title . "参赛作品分数.zip";
     * $pZip = new \ZipArchive();
     * $pZip->open($strPath, \ZipArchive::CREATE);
     * $strFilePath = $gen_dir;
     * $this->addFileToZip($strFilePath, $pZip);
     * $pZip->close();
     */
    if (!function_exists("add_file_to_zip")) {
        function add_file_to_zip($strPath, \ZipArchive $pZip)
        {
            $handler = opendir($strPath); //打开当前文件夹由$path指定。
            while (($filename = readdir($handler)) !== false) {
                if ($filename != "." && $filename != "..") {//文件夹文件名字为'.'和‘..’，不要对他们进行操作
                    if (is_dir($strPath . "/" . $filename)) {// 如果读取的某个对象是文件夹，则递归
                        $this->addFileToZip($strPath . "/" . $filename, $pZip);
                    } else { //将文件加入zip对象
                        if ($filename == 'background.jpg')
                            continue;
                        $pZip->addFile($strPath . "/" . $filename, $filename);
                    }
                }
            }
            @closedir($strPath);
            return true;
        }
    }

